<?php
use \Workerman\Autoloader;

require_once __DIR__ . '/../../../../../Workerman/Autoloader.php';
require_once __DIR__.'/../../../Config/Const.php';
Autoloader::setRootPath(__DIR__.'/../../..');

$page_groupId = GROUP_TREE_MNG;

require "../framepages/moudle_base.php";


$species_id = $_GET['species_id'];
$group_id = $_GET['group_id'];
$gg_cs = $_GET['gg_cs'];
$species_record = \Data\Tree_species_DAO::getRecord($species_id);
$species_name = $species_record->getSPECIES_NAME();
$gg_id = $species_record->getGG_ID();
$gg_record = \Data\Tree_gg_DAO::getRecord($gg_id);
$gg_name = $gg_record->getGG_NAME();
$gg_dw = $gg_record->getGG_DW();
$gg = $gg_name."(".$gg_dw.")";
$group_record = \Data\Tree_group_DAO::getRecord($group_id);
$group_name = $group_record->getGROUP_NAME();


$result = 'normal';
$mode = $_GET['mode'];
if ($mode == 'productor_add') {
    $province = $_GET['province'];
    $city = $_GET['city'];
    $county = $_GET['county'];
    $date = $_GET['date'];
    $price = $_GET['price'];
    // 同地区相同产品记录
    $productor_same_record = \Data\Tree_productor_DAO::getSameList($province,$city,$county,$group_id,$gg_cs);
    if ($productor_same_record) {
        $result = 'err_add_same';
    } else {
        $productor_record = new \Data\Tree_productor(array());
        $productor_record->setSPECIES_ID($species_id);
        $productor_record->setGROUP_ID($group_id);
        $productor_record->setGG_ID($gg_id);
        $productor_record->setGG_CS($gg_cs);
        $productor_record->setPRICE($price);
        $productor_record->setPROVINCE($province);
        $productor_record->setCITY($city);
        $productor_record->setCOUNTY($county);
        $productor_record->setDATE($date);
        $productor_record->setCREATED_BY($_SESSION['USER']);

        \Data\Tree_productor_DAO::insert($productor_record);
        $result = 'succ_add_productor';
    }

} elseif ($mode == 'delete_productor') {
    $productor_id = $_GET['productor_id'];
    $productor_record = \Data\Tree_productor_DAO::getRecord($productor_id);
    if ($productor_record) {
        \Data\Tree_productor_DAO::delete($productor_record);
        $result  = 'succ_delete_productor';
    } else {
        $result = 'err_delete_productor';
    }

} elseif ($mode == 'edit_productor') {
    $productor_id = $_GET['productor_id'];
    $price = $_GET['price'];
    $productor_record = \Data\Tree_productor_DAO::getRecord($productor_id);
    $productor_record ->setPRICE($price);
    \Data\Tree_productor_DAO::update($productor_record);
    $result = 'succ_edit';
}

?>
<div class="main-content">
    <div style="margin-bottom: 25px">
        <table>
            <tr>
                <th width="12%"><span>花木名称：</span></th>
                <td width="12%"><span><?php echo $species_name ?></span></td>
                <th width="12%"><span>分组名称：</span></span></th>
                <td><span><?php echo $group_name ?></span></span></td>
                <th width="12%"><span>规格类型：</span></th>
                <td width="12%"><span><?php echo $gg ?></span></td>
                <th width="12%"><span>规格参数：</span></th>
                <td width="12%"><span><?php echo $gg_cs ?></span></td>
            </tr>
        </table>

    </div>

    <div>
        <div>
            <table class="table" style="word-break:break-all">
                <tr>
                    <th width="7%">序号</th>
                    <th width="8%">产品id</th>
                    <th width="9%">省</th>
                    <th width="9%">市</th>
                    <th width="9%">区/县</th>
                    <th width="9%">本月询价次数</th>
                    <th width="9%">年/月</th>
                    <th width="9%">价格（元）</th>
                    <th width="9%">修改</th>
                    <th width="9%">删除</th>
                </tr>

                <?php
                $page_index = $_GET['page_index'];
                if (!$page_index) {
                    $page_index = 1;
                }
                $every_page_counts = 30;
                $list_start = ($page_index-1)*$every_page_counts;
                $productor_list_array = \Data\Tree_productor_DAO::getListByGroup_id($group_id,$gg_cs,$list_start,$every_page_counts);
                $productor_list = $productor_list_array[1];
                $total_count = $productor_list_array[0];
                $page_count = ceil($total_count/$every_page_counts);
                $page_last = $page_index-1;
                $page_next = $page_index+1;
                $i = $list_start+1;
                foreach ($productor_list as $productor_record) {
                    $productor_id = $productor_record->getPRODUCTOR_ID();
                    $province = $productor_record->getPROVINCE();
                    $city = $productor_record->getCITY();
                    $county = $productor_record->getCOUNTY();
                    $visit_count = $productor_record->getVISIT_COUNT();
                    $date = $productor_record->getDATE();
                    $price = $productor_record->getPRICE();
                    echo "
                        <tr>
                            <td>$i</td>
                            <td>$productor_id</td>                                             
                            <td>$province</td>                     
                            <td>$city</td>                     
                            <td>$county</td>                     
                            <td>$visit_count</td>                     
                            <td>$date</td>                     
                            <td><input type='text' id='price_$productor_id' value='$price' size='15'></td>                     
                            <td><button class=\"btn btn-danger\" onclick=\"edit_productor($productor_id)\">修改</button></td>                     
                            <td><button class=\"btn btn-danger\" onclick=\"delete_productor($productor_id)\">删除</button></td>                     
                        </tr>
                    ";
                    $i++;
                }
                ?>

            </table>
        </div>
        <div style="margin-left:35%; margin-top:40px;">
            <div>
                <?php
                if ($page_last) {
                    echo "            
                    <button onclick='last_page($page_last)'>上一页</button>                            
                ";
                }

                if ($page_next<=$page_count) {
                    echo "         
                    <button onclick='next_page($page_next)'>下一页</button>               
                ";
                }

                if ($page_count>1) {
                    echo "
                    &nbsp;&nbsp;&nbsp;&nbsp;去往第<input type='text' name='go_page' id='go_page' size='3'>页
                <button onclick='go_page()'>GO</button>
                    ";
                }
                ?>



            </div>

            <div style="margin-left:5%; margin-top:20px;">
                第<?php echo $page_index ?>页 &nbsp;&nbsp;&nbsp;&nbsp;
                共<?php echo $page_count ?>页  &nbsp;&nbsp;&nbsp;&nbsp;共<?php echo $total_count ?>条记录
            </div>


        </div>


    </div>
    <?php
    require "../framepages/footer.php";
    ?>

    <div>
        <div>
            <span><h2>添加到新地区</h2></span>
        </div>
        <div>
            <!--            <form method="post" action="">-->
            <div>
                <table>

                    <tr>
                        <td>价格(元)：</td>
                        <td><input type="text" name="price" id="price"></td>
                    </tr>
                    <tr>
                        <td>日期(年-月)：</td>
                        <td name="date" id="date"><?php echo date('Y-m') ?></td>
                    </tr>
                </table>
            </div>


            <div>
                <h5 class="title">请选择地区</h5>
                <div class="info">
                    <select name="province" id="province">
                        <option value="">省份</option>
                    </select>
                    <select name="city" id="city">
                        <option value="">地级市</option>
                    </select>
                    <select name="county" id="county">
                        <option value="">市、县级市</option>
                    </select>
                </div>
            </div>

            <div class="btn-toolbar list-toolbar">
                <button class="btn btn-danger" onclick="add_productor()">提交</button>

            </div>
            <!--            </form>-->
        </div>
    </div>


</div>
</div>
<script src="../lib/location.js"></script>
<script src="../lib/bootstrap/js/bootstrap.js"></script>
<script>
    function init() {
        var msg = '<?php echo $result ?>';
        if (msg == 'succ_add_productor') {
            alert('添加成功');
        }
        if (msg == 'succ_delete_productor') {
            alert('删除成功');
        }

        if (msg == 'err_delete_productor') {
            alert('删除失败');
        }
        if (msg == 'succ_edit') {
            alert('修改成功');
        }
        if (msg == 'err_add_same') {
            alert('添加失败，该地区已存在该产品')
        }



        var mode = "<?php echo $mode ?>";
        if (mode) {
            var url = "productor_list.php?species_id="+"<?php echo $species_id ?>"+"&group_id="+"<?php echo $group_id ?>"+"&gg_cs="+"<?php echo $gg_cs ?>";
            window.location.href = url;
        }

        var pro=$("#province");
        for(var i=0;i<province.length;i++){
            var $temp=$("<option value=\"\">"+province[i].name+"</option>");
            pro.append($temp);
        }

    }

    function add_productor() {
        var province = $("#province option:selected").text();  //获取选中的项
        var city = $("#city option:selected").text();  //获取选中的项
        var county = $("#county option:selected").text();  //获取选中的项


        var price = document.getElementById('price').value;
        var date = "<?php echo date('Y-m') ?>";
        if (province.length==0 || province == '省份') {
            alert('请选择省');
        } else if (city.length==0 || city == '地级市') {
            alert('请选择市');
        } else if (county.length==0 || county == '市、县级市') {
            alert('请选择区/县');
        } else if (price.length==0) {
            alert ('请输入价格');
        } else if (date.length==0) {
            alert ('请输入日期');
        } else {
            var url = "productor_list.php?species_id="+"<?php echo $species_id ?>"+"&group_id="+"<?php echo $group_id ?>"+"&gg_cs="+"<?php echo $gg_cs ?>";
            url = url+"&mode=productor_add&province="+province+"&city="+city+"&county="+county+
                "&date="+date+"&price="+price;
            window.location.href = url;
        }
    }

    function delete_productor(productor_id) {
        var url = "productor_list.php?species_id="+"<?php echo $species_id ?>"+"&group_id="+"<?php echo $group_id ?>"+"&gg_cs="+"<?php echo $gg_cs ?>";

        url = url+"&mode=delete_productor&productor_id="+productor_id;
        window.location.href = url;
    }


    function edit_productor(productor_id) {
        var price = document.getElementById('price_'+productor_id).value;
        if (!productor_id) {
            alert('修改失败，未找到相应产品');
        } else if (!price) {
            alert('修改失败，请输入价格');
        } else {
            var url = "productor_list.php?species_id="+"<?php echo $species_id ?>"+"&group_id="+"<?php echo $group_id ?>"+"&gg_cs="+"<?php echo $gg_cs ?>";

            url = url+"&mode=edit_productor&productor_id="+productor_id+"&price="+price;
            window.location.href = url;
        }
    }

    function last_page(page_last) {
        url = "?page_index="+page_last+"&species_id="+"<?php echo $species_id ?>"+"&group_id="+"<?php echo $group_id ?>"+"&gg_cs="+"<?php echo $gg_cs ?>";
        window.location.href = url;
    }

    function next_page(page_next) {
        url = "?page_index="+page_next+"&species_id="+"<?php echo $species_id ?>"+"&group_id="+"<?php echo $group_id ?>"+"&gg_cs="+"<?php echo $gg_cs ?>";
        window.location.href = url;
    }

    function go_page()
    {
        url = "?page_index="+$('#go_page').val()+"&species_id="+"<?php echo $species_id ?>"+"&group_id="+"<?php echo $group_id ?>"+"&gg_cs="+"<?php echo $gg_cs ?>";
        window.location.href = url;
    }


    $(function(){
        var pro=$("#province");
        var city=$("#city");
        var dAC=$("#county");
        var num1=null;
        var num2=null;
        //init();

        pro.change(function(){
            num1=pro.prop("selectedIndex");
            city.find("option").eq(0).siblings().remove();
            dAC.find("option").eq(0).siblings().remove();
            if(num1!==0){
                for(var i=0;i<province[num1-1].city.length;i++){
                    var $temp=$("<option value=\"\">"+province[num1-1].city[i].name+"</option>");
                    city.append($temp);
                }
            }
        });

        city.change(function(){
            num2=city.prop("selectedIndex");
            dAC.find("option").eq(0).siblings().remove();
            if(num2!==0){
                for(var i=0;i<province[num1-1].city[num2-1].districtAndCounty.length;i++){
                    var $temp=$("<option value=\"\">"+province[num1-1].city[num2-1].districtAndCounty[i]+"</option>");
                    dAC.append($temp);
                }
            }
        })
    });
</script>

</body>
</html>
